import { apiOne } from "./api";
import * as echarts from "./echarts.min.js";

// 用柱状图展示接口所有数据中各设备各个状态持续时长（秒）
//版本1
const oneboxfn = async () => {
  let datastart = [];
  let datadown = [];
  let datastop = [];
  let dataname = [];
  let dataobj = {};
  let datasum = {};
  let data = (await apiOne()).data.data;
  data.forEach((element) => {
    dataobj[element.machineName] = element.showFactChangeRecordList;
  });
  for (const key in dataobj) {
    let linsiline = {};
    linsiline["运行"] = 0;
    linsiline["待机"] = 0;
    linsiline["离线"] = 0;
    dataobj[key].forEach((e) => {
      let time =
        (new Date(e.changeEndTime).getTime() -
          new Date(e.changeStartTime).getTime()) /
        1000;
      linsiline[e.changeRecordState] = linsiline[e.changeRecordState] + time;
    });
    datasum[key] = linsiline;
    dataname.push(key);
    for (const key in linsiline) {
      if (key == "运行") {
        datastart.push(linsiline[key]);
      }
      if (key == "待机") {
        datadown.push(linsiline[key]);
      }
      if (key == "离线") {
        datastop.push(linsiline[key]);
      }
    }
  }
  console.log(datasum);
  console.log(dataname, datastart, datadown, datastop);
  let MyEcharts = echarts.init(document.getElementById("mainDiv"));
  MyEcharts.setOption({
    title: {
      text: "各设备各个状态持续时长",
    },
    xAxis: {
      name: "设备名",
      data: dataname,
    },
    yAxis: {
      name: "持续时间长",
    },
    tooltip: {},
    series: [
      {
        type: "bar",
        data: datastart,
        name: "运行",
      },
      {
        type: "bar",
        data: datadown,
        name: "待机",
      },
      {
        type: "bar",
        data: datastop,
        name: "离线",
      },
    ],
  });
};

// 用柱状图展示接口所有数据中各设备各个状态持续时长（秒）
//版本2
const barOne = async () => {
  let showFactChangeRecordListdata = [];
  let changeRecordStateobj = {};
  let showname = [];
  let showdatastart = [];
  let showdatastop = [];
  let showdatadrp = [];
  let showdataobj = {};
  let data = (await apiOne()).data.data;
  // console.log(data)
  data.forEach((element) => {
    showFactChangeRecordListdata.push({
      name: element.machineName,
      data: element.showFactChangeRecordList,
      zahungtai: {},
    });
  });
  showFactChangeRecordListdata.forEach((element) => {
    element.data.forEach((e) => {
      changeRecordStateobj[e.changeRecordState] = 0;
    });
  });
  showFactChangeRecordListdata.forEach((e) => {
    for (const key in changeRecordStateobj) {
      e.zahungtai[key] = 0;
      showdataobj[key] = [];
    }
  });
  showFactChangeRecordListdata.forEach((element) => {
    showname.push(element.name);
    element.data.forEach((e) => {
      let time =
        (new Date(e.changeEndTime).getTime() -
          new Date(e.changeStartTime).getTime()) /
        1000;
      // console.log(e.changeRecordState)
      element.zahungtai[e.changeRecordState] =
        element.zahungtai[e.changeRecordState] + time;
    });
  });
  showFactChangeRecordListdata.forEach((element) => {
    for (const key in element.zahungtai) {
      showdataobj[key].push(element.zahungtai[key]);
    }
  });
  for (const key in showdataobj) {
    if (key == "运行") {
      showdatastart.push(...showdataobj[key]);
    }
    if (key == "待机") {
      showdatadrp.push(...showdataobj[key]);
    }
    if (key == "离线") {
      showdatastop.push(...showdataobj[key]);
    }
  }
  console.log(showname, showdatastart, showdatadrp, showdatastop);
  let myEcharts = echarts.init(document.getElementById("mainDiv"));
  myEcharts.setOption({
    title: {
      text: "各设备各个状态持续时长",
    },
    xAxis: {
      name: "设备名",
      data: showname,
    },
    yAxis: {
      name: "持续时间长",
    },
    tooltip: {},
    series: [
      {
        type: "bar",
        data: showdatastart,
        name: "运行",
      },
      {
        type: "bar",
        data: showdatadrp,
        name: "待机",
      },
      {
        type: "bar",
        data: showdatastop,
        name: "离线",
      },
    ],
  });
};

// 用柱状图展示接口所有数据中各设备各个状态持续时长（秒）
//最终版
const barDemo = async () => {
  let data = (await apiOne()).data.data;
  let datastart = [];
  let datastop = [];
  let datadown = [];
  let dataname = [];
  let datasum = {};
  console.log(data);
  data.forEach((element) => {
    datasum[element.machineName] = {
      运行: 0,
      待机: 0,
      离线: 0,
    };
    element.showFactChangeRecordList.forEach((e) => {
      let time =
        (new Date(e.changeEndTime) - new Date(e.changeStartTime)) / 1000;
      datasum[element.machineName][e.changeRecordState] =
        datasum[element.machineName][e.changeRecordState] + time;
    });
  });
  console.log(datasum);
  for (const key in datasum) {
    dataname.push(key);
    datastart.push(datasum[key]["运行"]);
    datadown.push(datasum[key]["待机"]);
    datastop.push(datasum[key]["离线"]);
  }
  console.log(dataname, datastart, datadown, datastop);
  let MyEcharts = echarts.init(document.getElementById("mainDiv"));
  MyEcharts.setOption({
    title: {
      text: "设备各个状态持续时长（秒）",
    },
    xAxis: {
      name: "设备名称",
      data: dataname,
    },
    tooltip: {},
    yAxis: {
      name: "持续时间",
    },
    series: [
      {
        type: "bar",
        name: "运行",
        data: datastart,
      },
      {
        type: "bar",
        name: "待机",
        data: datadown,
      },
      {
        type: "bar",
        name: "离线",
        data: datastop,
      },
    ],
  });
};

// 用柱状图展示每日所有车间各设备平均运行时长
// （秒，四舍五入保留两位小数），
// 每日里的所有设备按照接口返回的数据顺序展示即可
const barTwo = async () => {
  let data = (await apiOne()).data.data;
  console.log(data);
  let dataindex = {};
  let datasum = {};
  let dataobj = {};
  let namedata = [];
  let sujudata = [];
  let date = [];
  let datadata = data.map((e) => {
    dataobj[e.machineName] = {};
    return {
      name: e.machineName,
      data: e.showFactChangeRecordList.map((e1) => {
        if (e1.changeRecordState == "运行") {
          let time =
            (new Date(e1.changeEndTime) - new Date(e1.changeStartTime)) / 1000;
          let day = new Date(e1.changeStartTime).getDate();
          dataindex[day] = 0;
          datasum[day] = 0;
          return { yunxing: time, ri: day };
        }
      }),
    };
  });
  datadata.forEach((e) => {
    e.data.forEach((e1) => {
      if (e1 != undefined) {
        for (const key in dataindex) {
          if (key == e1.ri) {
            dataindex[key] += 1;
            datasum[key] = datasum[key] + e1.yunxing;
          }
        }
      }
    });
    for (const key in dataindex) {
      let linsi;
      if (datasum[key] != 0) {
        linsi = (datasum[key] / dataindex[key]).toFixed(2);
      } else {
        linsi = 0;
      }
      dataobj[e.name][key] = linsi;
    }
    for (const key in dataindex) {
      dataindex[key] = 0;
      datasum[key] = 0;
    }
  });
  console.log(dataobj);
  //   12: '4071.71', 13: '5312.08', 14: '22.00', 15: '3920.13'
  let arr12 = [];
  let arr13 = [];
  let arr14 = [];
  let arr15 = [];
  for (const key in dataobj) {
    namedata.push(key)
    for (const key1 in dataobj[key]) {
      if (key1 == 12) {
        arr12.push(dataobj[key][key1]);
      }
      if (key1 == 13) {
        arr13.push(dataobj[key][key1]);
      }
      if (key1 == 14) {
        arr14.push(dataobj[key][key1]);
      }
      if (key1 == 15) {
        arr15.push(dataobj[key][key1]);
      }
    }
  }
  console.log(namedata,arr12, arr13,arr14,arr15);
  let MyEcharts = echarts.init(document.getElementById("mainDiv"));
  MyEcharts.setOption({
    title: {
      text: "每日所有车间各设备平均运行时长",
    },
    xAxis: {
      name: "设备名称",
      data: namedata,
    },
    tooltip: {},
    yAxis: {
      name: "平均运行时间",
    },
    series: [
      {
        type: "bar",
        name: "12",
        data: arr12,
      },
      {
        type: "bar",
        name: "13",
        data: arr13,
      },
      {
        type: "bar",
        name: "14",
        data: arr14,
      },
      {
        type: "bar",
        name: "15",
        data: arr15,
      }
    ],
  });
};
export const mainFn = () => {
  // barOne();
  // oneboxfn()
  // barDemo()
  barTwo();
};
